package com.whsxt.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.Constants;
import com.whsxt.domain.Car;
import com.whsxt.domain.Rent;
import com.whsxt.domain.Return;
import com.whsxt.dto.RentDto;
import com.whsxt.exception.BusinessException;
import com.whsxt.mapper.CarMapper;
import com.whsxt.mapper.RentMapper;
import com.whsxt.mapper.ReturnMapper;
import com.whsxt.service.RentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class RentServiceImpl implements RentService {

    @Autowired
    private RentMapper rentMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private ReturnMapper returnMapper;

    /**
     * 获取租车记录的分页数据
     * @param rentDto
     * @return
     */
    @Override
    public PageInfo<Rent> select(RentDto rentDto) {
        Page<Object> objects = PageHelper.startPage(rentDto.getPage(), rentDto.getLimit());
        List<Rent> rentList = rentMapper.selectAll(rentDto);
        PageInfo<Rent> rentPageInfo = new PageInfo<>(rentList);
        rentPageInfo.setTotal(objects.getTotal());
        return rentPageInfo;
    }

    /**
     * 批量删除租车记录
     * @param ids
     * @return
     */
    @Override
    public boolean batchDelete(List<Integer> ids) {
        return rentMapper.batchDelete(ids) > 0;
    }

    /**
     * 清空租车记录
     *
     * @return
     */
    @Override
    public boolean clear() {
        return rentMapper.deleteAll() > 0;
    }


    /**
     * 计算租赁消费情况
     *
     * @param id
     * @return
     */
    @Override
    public BigDecimal getPayMoney(Integer id) {
        Rent rent = rentMapper.selectByPrimaryKey(id);
        // 获取租赁结束时间
        String endTime = rent.getEndTime();
        // 获取租赁开始时间
        String beginTime = rent.getBeginTime();

        //计算两个时间之间的时长(使用hutool)
        DateTime end = DateTime.of(endTime, "yyyy-MM-dd HH:mm:ss");
        DateTime start = DateTime.of(beginTime, "yyyy-MM-dd HH:mm:ss");
        // 计算相差多长时间
        long minutes = DateUtil.between(start, end, DateUnit.MINUTE);

        // 计算价格 四舍五入六成双
        BigDecimal times = BigDecimal.valueOf(minutes).divide(BigDecimal.valueOf(60), 2, BigDecimal.ROUND_HALF_EVEN);
        return times.multiply(BigDecimal.valueOf(rent.getRentPrice()));
    }

    /**
     * 还车的业务
     *
     * @param id 租车记录的id
     * @return 是否还车成功
     */
    @Override
    @Transactional
    public boolean returnCar(Integer id) {

        Rent rent = rentMapper.selectByPrimaryKey(id);
        // 通过车牌查询车辆信息
        String num = rent.getNum();
        Car car = carMapper.selectByNum(num);

        // 向还车表插入记录
        Return aReturn = new Return();
        // 赋值共有属性
        BeanUtils.copyProperties(rent, aReturn);
        // 设置默认值
        aReturn.setId(null);
        aReturn.setRentId(rent.getId());
        aReturn.setReturnTime(rent.getEndTime());

        aReturn.setProblem("没有问题");
        // 出租的支付
        aReturn.setTotalMoney(getPayMoney(id).intValue());
        // 总支付 = 出租的支付 + 出问题的支付
        aReturn.setPayMoney(aReturn.getTotalMoney() + 0);
        aReturn.setCreateTime(new Date());

        int insert = returnMapper.insert(aReturn);

        if(insert < 0){
            throw new BusinessException(CodeMsg.RETURN_RECORD_ADD_FAILED);
        }

        // 修改租车记录表的状态
        Rent updateRent = new Rent();
        updateRent.setId(rent.getId());
        updateRent.setFlag(Constants.CAR_RETURNED);
        int updateRentRows = rentMapper.updateByPrimaryKeySelective(updateRent);
        if(updateRentRows <= 0){
            throw new BusinessException(CodeMsg.RENT_STATUS_UPDATE_FAILED);
        }

        // 修改车辆表的车辆出租的状态
        Car updateCar = new Car();
        updateCar.setId(car.getId());
        updateCar.setIsRent(Constants.DEFAULT_CAR_RENT_STATUS);

        int update = carMapper.updateByPrimaryKeySelective(updateCar);
        if(update <= 0){
            throw new BusinessException(CodeMsg.CAR_RENT5_STATUS_UPDATE_FAILED);
        }
        return true;
    }
}
